package mygui

import (
	//"image/color"
	"fyne.io/fyne/v2"
	//"fyne.io/fyne/v2/app"
	//"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	//"fyne.io/fyne/v2/dialog"
	//"os"
	"fmt"
	"encoding/json"
	//"github.com/flopp/go-findfont"
	//"strings"
	//"reflect"
)

/*
func init() {
    fontPaths := findfont.List()
    for _, path := range fontPaths {
        //fmt.Println(path)
        //楷体:simkai.ttf
        //黑体:simhei.ttf
        if strings.Contains(path, "simkai.ttf") {
            fmt.Println(path)
            os.Setenv("FYNE_FONT", path) // 设置环境变量  // 取消环境变量 os.Unsetenv("FYNE_FONT")
            break
        }
    }
    fmt.Println("=============")
}
*/

func White_main(myApp fyne.App, t_data_s chan int) fyne.Window {
	var ladata = []string{"游戏Fan", "联想", "九游","一元"}
	var vadata = []string{}
	var t_data = make(chan int)
	//fmt.Println(t_data)
	//myApp := app.New()
	myWindow := myApp.NewWindow("游戏白名单设置")

	input := widget.NewEntry()
	input.SetPlaceHolder("游戏名字")

	lalist := widget.NewSelect(ladata, func(value string) {
		//b_data <- 3
		//fmt.Println(value)
	})
	valist := widget.NewSelectEntry(vadata)
	//obj_dir(valist)
	//valist.SetSelectedIndex(0)
	lalist.SetSelectedIndex(0)

	button1 := widget.NewButton("添加", func() {
		
		t_data <- 1
	})
	button2 := widget.NewButton("移除", func() {
		t_data <- 2
	})
	button3 := widget.NewButton("查询", func() {
		t_data <- 3
	})
	but := container.New(layout.NewHBoxLayout(),button1, layout.NewSpacer(), button3, layout.NewSpacer(),  button2)
	box := container.New(layout.NewVBoxLayout(), input, layout.NewSpacer(), lalist, layout.NewSpacer())
	mbox := container.New(layout.NewVBoxLayout(), valist, layout.NewSpacer(), box, layout.NewSpacer(), but)
	myWindow.SetContent(mbox)
	myWindow.SetCloseIntercept(func(){
		fmt.Println("禁止关闭窗口")
		//myWindow.Hide()
		t_data_s <- 8
	})
	//myWindow.Resize(fyne.NewSize(200, 200))
	//myWindow.CenterOnScreen()
	//myWindow.ShowAndRun()
	go func() {
		call := func(mbool bool) {
		}
		for {
			us := <- t_data
			switch us {
				case 1:
					button1.Disable()
					text := str_curt(input.Text)
					if text != "" {
						_, err := http_data(us, text, ladata[lalist.SelectedIndex()])
						if err == nil {
							vadata = append(vadata, text)
							valist.SetOptions(vadata)
							M_dialog("添加提示", "添加成功", call, myWindow)
						}else{
							//dialog.NewEntryDialog("提示","添加失败")
							fmt.Println("添加失败")
							M_dialog("添加提示", "添加失败，网络错误", call, myWindow)
						}
					}else{
						M_dialog("添加提示", "添加失败，添加游戏名字错误", call, myWindow)
					}
					button1.Enable()
				case 2:
					button2.Disable()
					//fmt.Println(valist.SelectedText())
					_, err := http_data(us,valist.Text,ladata[lalist.SelectedIndex()])
					//fmt.Println(data)
					if err == nil {
						M_dialog("删除提示", "删除成功", call, myWindow)
						index := indexOf(valist.Text,vadata)
						if index == -1 {
							fmt.Println("0")
						}else{
							vadata = append(vadata[:index], vadata[index+1:]...)
							valist.SetOptions(vadata)
						}
					}else{
						//dialog.NewEntryDialog("提示","数据库中不存在")
						fmt.Println("删除失败")
						M_dialog("删除提示", "删除失败，网络错误", call, myWindow)
					}
					button2.Enable()
				case 3:
					button3.Disable()
					data, err := http_data(us,"查",ladata[lalist.SelectedIndex()])
					if err == nil {
						data = `{"data":` + data + `}`  //接口返回补充成golang的json结构用于json解析
						//fmt.Println(data)
						var basket JsonList
						json.Unmarshal([]byte(data), &basket)
						//fmt.Println(basket.Data[0].Name)
						vadata = []string{}
						for _, v := range basket.Data {
							//fmt.Println(v)
							vadata = append(vadata, v.Name)
							v.Id = len(vadata)
						}
						valist.SetOptions(vadata)
					}
					button3.Enable()

			}
		}
	}()
	
	return myWindow
}


type JsonList struct {
	Data 	 []JsonData	`json:"data"`
}

type JsonData struct {
    Name     string 	`json:"game"`
    Time   	 string 	`json:"time"`
    Channel  string  	`json:"channel"`
    Id       int
}
